<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Position;
use App\Transformers\PositionTransformer;

class PositionController extends Controller
{
    //
    /**
     * 位置列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request) {
        $bistro = $request->user('bistro');

        $name = $request->input('name');
        $start = $request->input('start');
        $end = $request->input('end');

        $positions = Position::where('bistro_id', $bistro->id)->name($name)->servantCount($start, $end)->orderby('id', 'desc')->paginate(10);
        $data['data'] = [
            'positions' => $this->factalPaginator($positions, new PositionTransformer())
        ];
        return $this->apiSuccess($data);
    }

    /**
     * 新增位置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request) {
        $bistro = $request->user('bistro');
        $input = $request->all();
        $validateResult = $this->validateCreatePosition($input);
        if ($validateResult["errorCode"] > 0) return $this->apiError($validateResult["message"], $validateResult["errorCode"]);
        $position = $this->_setPosition(new Position(), $input, $bistro);
        $position->qr_code = $bistro->id.uniqid();

        $wechat = app('wechat.mini_program');

        // 生成A类小程序码（10万个数量上限）
        $response = $wechat->app_code->get('pages/index?qr_code=' . $position->qr_code, [
            'width' => 300,
            'line_color' => [
                'r' => 51,
                'g' => 51,
                'b' => 51 ,
            ],
        ]);

        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
//            $filename = $response->saveAs(public_path('uploads/images/qr_code/'), $bistro->id.'|'.$position->qr_code.'.png');
            $filename = $response->saveAs(public_path('uploads/images/qr_code/'), $bistro->id.'|'.$position->qr_code.'.png');
//            $user->qr_code = 'uploads/images/qr_code/' . $filename;
//            $user->save();
//            return $this->responseA('成功生成二维码', 200, ['url' => $user->qr_code]);
        }
        $position->wx_qr_code = 'uploads/images/qr_code/' . $filename;
        $position->save();

        $data['data'] = [
            'position' => $this->fractalItem($position, new PositionTransformer())
        ];
        return $this->apiSuccess($data);
    }

    /**
     * 位置详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id) {
        $position = Position::findorfail($id);
        $data['data'] = [
            'position' => $this->fractalItem($position, new PositionTransformer())
        ];
        return $this->apiSuccess($data);
    }

    /**
     * 修改位置
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request, $id) {
        $bistro = $request->user('bistro');
        $input = $request->all();
        $position = Position::findorfail($id);
        $position = $this->_setPositionForUpdate($position, $input);
        $position->save();
        $data['data'] = [
            'position' => $this->fractalItem($position, new PositionTransformer())
        ];
        return $this->apiSuccess($data);
    }

    /**
     * 删除位置
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request, $id) {
        $bistro = $request->user('bistro');

        $position = Position::where('bistro_id', $bistro->id)->where('id', $id)->first();
        if(empty($position)) return $this->apiError('未找到指定信息');
        $position->delete();

        return $this->apiSuccess();
    }

    private function _setPositionForUpdate($position, $input) {
        $position->name = $input['name'];
        $position->servant_count = $input['servant_count'];
        return $position;
    }

    private function _setPosition($position, $input, $bistro) {
        $position->bistro_id = $bistro->id;
        $position->name = $input['name'];
//        $position->qr_code = $input['qr_code'];
        $position->servant_count = $input['servant_count'];
        return $position;
    }

    private function validateCreatePosition($input) {
        $name = isset($input['name']) ? $input['name']: '';
//        $qr_code = isset($input['qr_code']) ? $input['qr_code']: '';
        $servant_count = isset($input['servant_count']) ? $input['servant_count']: '';

        if(empty($name)){
            return [
                "message" => "点名不得为空",
                "errorCode" => 5000
            ];
        }

//        if(empty($qr_code)){
//            return [
//                "message" => "二维码不得为空",
//                "errorCode" => 5000
//            ];
//        }

        if(empty($servant_count)){
            return [
                "message" => "可座人数不得为空",
                "errorCode" => 5000
            ];
        }

        return [
            "message" => "没有错误信息",
            "errorCode" => 0
        ];

    }
}
